package caidan;

import caidan.res.Resource;
import jinyilw.common.app.AppConfig;
import jinyilw.swing.ComponentTools;
import jinyilw.swing.FontTools;
import jinyilw.swing.ResourceManager;
import jinyilw.swing.image.ImageTools;
import jinyilw.swing.visuals.Icomp.IRichWrapLabel;
import jinyilw.swing.visuals.ImageWindow;
import jinyilw.swing.visuals.Jcomp.TransRichButton;
import jinyilw.swing.visuals.Rcomp.RDialog;
import jinyilw.swing.visuals.border.RTitledBorder;
import jinyilw.swing.visuals.text.WrapLabel;

import javax.swing.SwingConstants;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class AboutDialog extends RDialog
{
	private static final long serialVersionUID = -927671352131814824L;

	private final MainFrame mainFrame;
	private IRichWrapLabel softPanel;
	private WrapLabel infoPane1;
	private WrapLabel infoPane2;
	private WrapLabel infoPane3;
	TransRichButton softVersion = new TransRichButton(
			"软件版本号：" + AppConfig.appVersion);
	TransRichButton wxButton = new TransRichButton("如有好的意见建议，可微信联系");
	ImageWindow wxWindow;
	Font txtFont = FontTools.getSystemFont(19);
	Font wordFont = FontTools.getSystemFont(17);
	String longText1 = "      "
			+ "单击菜单标签即可进行选择，再次单击则可取消选择；或者单击菜单图片放大，依次浏览并进行选择；先调整时间并按照设定的数量选好菜单，然后提交选择。";
	String longText2 = "      可将当前统计数据保存为数据文件，或者直接恢复上次关闭时的统计数据；也可将当前的统计数据与以前的统计数据进行合并，方便统计数据的汇总。";
	String longText3 = "      根据统计数据情况生成并显示最喜欢的菜单和一周推荐菜单，可对一周菜单进行编辑，并将食谱数据导出为文本文件和Excel表格文件。";
	String longText = "      使用《食谱管理系统》可形象直观地进行点餐并统计菜单数据，为食谱制定提供参考和辅助。主要具有菜单选择功能、数据统计功能和食谱编辑功能。";

	public AboutDialog(MainFrame mainFrame)
	{
		super(mainFrame);
		this.mainFrame = mainFrame;
		setSize(780, 580);
		setResizable(false);
		setLocationRelativeTo(null);
		initGUI();
	}

	private void initGUI()
	{
		setOptionStyle(80, 0.92f);
		Container contentPanel = getContentPane();
		contentPanel.setLayout(null);
		addMouseListener(new ThisMouseListener());
		setTitleHeight(28);
		setTitleFont(FontTools.getCustomFont(22));
		setTitle("软件说明");
		setWindowSymbol(Resource.starIcon);

		softPanel = new IRichWrapLabel(longText, 640, txtFont);
		contentPanel.add(softPanel);
		softPanel.setHorizontalAlignment(SwingConstants.CENTER);
		softPanel.setForeground(Color.RED.brighter());
		softPanel.setBounds(50, 20, 685, 120);

		Font titleFont = FontTools.getCustomFont(19);
		infoPane1 = new WrapLabel(longText1, 640, wordFont);
		contentPanel.add(infoPane1);
		infoPane1.setHorizontalAlignment(SwingConstants.CENTER);
		infoPane1.setBorder(
				new RTitledBorder("菜单选择功能", titleFont, Color.yellow));
		infoPane1.setBounds(50, 130, 680, 110);

		infoPane2 = new WrapLabel(longText2, 640, wordFont);
		contentPanel.add(infoPane2);
		infoPane2.setHorizontalAlignment(SwingConstants.CENTER);
		infoPane2.setBorder(
				new RTitledBorder("数据统计功能", titleFont, Color.yellow));
		infoPane2.setBounds(50, 250, 680, 110);

		infoPane3 = new WrapLabel(longText3, 640, wordFont);
		contentPanel.add(infoPane3);
		infoPane3.setHorizontalAlignment(SwingConstants.CENTER);
		infoPane3.setBorder(
				new RTitledBorder("食谱编辑功能", titleFont, Color.yellow));
		infoPane3.setBounds(50, 370, 680, 110);

		contentPanel.add(softVersion);
		softVersion.setForeground(Color.yellow);
		softVersion.setFont(FontTools.getLargeFont());
		softVersion.setBounds(110, 500, 200, 30);
		softVersion.setCursor(ComponentTools.handCursor);
		softVersion.addClickOperater((Object o) ->
		{
			new Thread(() ->
			{
				synchronized (softVersion)
				{
					mainFrame.checkUpdate();
				}
			}).start();
		});

		contentPanel.add(wxButton);
		wxButton.setForeground(Color.yellow);
		wxButton.setFont(FontTools.getLargeFont());
		wxButton.setCursor(ComponentTools.handCursor);
		wxButton.setBounds(370, 500, 300, 30);
		wxButton.addClickOperater((Object o) ->
		{
			if (wxWindow == null)
			{
				wxWindow = new ImageWindow(ImageTools.toBufferedImage(
						ResourceManager.getImage(AppConfig.class, "wx.jpg")));
				wxWindow.getContentPane().addMouseListener(new MouseAdapter()
				{
					@Override
					public void mouseClicked(MouseEvent e)
					{
						wxWindow.dispose();
					}
				});
				wxWindow.setAlwaysOnTop(true);
				wxWindow.setLocationRelativeTo(null);
			}
			if (wxWindow.isVisible())
				wxWindow.dispose();
			else
				wxWindow.setVisible(true);
		});
	}

	private class ThisMouseListener extends MouseAdapter
	{
		@Override
		public void mouseClicked(MouseEvent e)
		{
			dispose();
		}
	}
}
